package com.library.management.controller;

import com.library.management.dto.OnlineUserDTO;
import com.library.management.service.SessionsService;
import com.library.management.util.Result;
import cn.dev33.satoken.annotation.SaCheckRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会话管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/sessions")
@Api(tags = "会话管理接口", description = "提供在线用户查询和强制登出功能")
public class SessionController {

    @Autowired
    private SessionsService sessionsService;

    /**
     * 获取在线用户列表
     * @return 在线用户列表
     */
    @ApiOperation("获取在线用户列表")
    @SaCheckRole("admin")
    @GetMapping("/online")
    public Result<List<OnlineUserDTO>> getOnlineUsers() {
        log.info("获取在线用户列表");
        List<OnlineUserDTO> onlineUsers = sessionsService.getOnlineUsers();
        return Result.success(onlineUsers);
    }

    /**
     * 强制用户退出
     * @param userId 用户ID
     * @return 操作结果
     */
    @ApiOperation("强制用户退出")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @SaCheckRole("admin")
    @PostMapping("/logout/{id}")
    public Result<Boolean> forceLogout(@PathVariable("id") Integer userId) {
        log.info("强制用户退出: userId={}", userId);
        boolean result = sessionsService.forceLogout(userId);
        return Result.success(result, "用户已成功强制下线");
    }
} 